<?php

/*
 * Exception Emailer Class
 * ===============================================
 * Date: May 20th, 2012
 * Created By: Integration Team
 * ===============================================
 */

/**
 * Exception emailing class
 * Extends the Emailing class and is used only 
 * by the Custom Exception class - This is 
 * required in order to email any exceptions
 * including Email exceptions which might be 
 * caused due to mis-configured Email settings 
 * for an instance 
 * 
 */

require_once 'email/class.phpmailer.php';


class ExceptioneMailer extends eMailer{
    
    private $toEmails = "bspereira@etilize.com,bertram.pereira@gmail.com";
    private $emailTemplate = "There was an Exception processing one of the scripts<br/>";

    public function __construct($body="") 
    {
        //Do not pick up the settings from the configuration file
        //The config file might have incorrect config and needs to 
        //be mitigated when the Exception email is being sent
     
            $this->SMTPsecure = 'ssl';
            $this->host = 'smtp.gmail.com';
            $this->port = 465; //465 for SSL and 587 for TLS
            $this->SMTP_AUTH = TRUE;
            $this->userName = 'etilize.integration@gmail.com';
            $this->password = '1nt3gr@t3';
            $this->fromAddress = 'etilize.integration@gmail.com';
            $this->fromName = 'PHP Export Script';
            $this->subject = 'PHP Export Script';
            $this->Email_Addresses = 'integrate@etilize.com';
    }    
    
    public function sendEmail($emailBody)
    {
        $mailer = new PHPMailer;
        $mailer->IsSMTP();
        $mailer->SMTPSecure = $this->SMTPsecure;
        $mailer->Host = $this->host;
        $mailer->Port = $this->port;
        $mailer->SMTPAuth = $this->SMTP_AUTH;
        $mailer->Username = $this->userName;
        $mailer->Password = $this->password;
        $mailer->From = $this->fromAddress;
        $mailer->FromName = $this->fromName;
        $mailer->Subject = $this->subject;
        $mailer->IsHTML();
        $mailer->Body = $this->emailTemplate.$emailBody."<br/> Script:".$this->php_self();
        
        
        if($this->toEmails=="")
        {
            throw new Exception("No Email Address Specified");
        }
        else
        {
            $addresses = explode(',', $this->toEmails);
            foreach($addresses as $email)
            {
                $mailer->AddAddress($email);
     
            }
                
                 if(!$mailer->Send())
                 {
                     throw new Exception("Could Not send Exception Email");
                 }
        }
    }
    private function php_self()
    {
        //function to get which script was run when this 
        //exception occured 
        
        $bt = debug_backtrace();
        return $bt[count($bt)-1]['file'];
    }
    
}

?>
